package com.ruoyi.core.productuser;


import com.ruoyi.core.domain.ClientProductUser;
import com.ruoyi.core.service.impl.ClientOperationServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;

public class ClientProductUserPollStrategy {

    private static int INDEX;

    private static HashMap<Long,Integer> indexMap = new HashMap<>();

    private static List<ClientProductUser> CLIENT_PRODUCT_USERS;


    public static ClientProductUser get(Long productId) {
        Assert.notNull(CLIENT_PRODUCT_USERS,"客户端内置账号不允许为空");

        int index = getIndex(productId) % CLIENT_PRODUCT_USERS.size();
        return CLIENT_PRODUCT_USERS.get(index);
    }

    private static Integer getIndex(Long productId){
        Integer index = indexMap.computeIfAbsent(productId, k -> CLIENT_PRODUCT_USERS.size());
        increaseIndex(productId,index + 1);
        return index;
    }

    public static void clearIndexCache(){
        indexMap.clear();
    }

    private static void increaseIndex(Long productId,Integer index){
        indexMap.put(productId,index);
    }

    public static void setClientProductUsers(List<ClientProductUser> clientProductUsers) {
        ClientProductUserPollStrategy.CLIENT_PRODUCT_USERS = clientProductUsers;
    }
}
